import kvStoreUtil from '../../../kvStore/kvStoreUtil';
import log from '../../../logutil';
import api from '../../api';
import mysApiMagr from '../mysApiMagr';
import getGameRecordCard from './getGameRecordCard';
export default class dailyNoteApi extends api{
  public static async getInstance():Promise<dailyNoteApi>{
    let server = await kvStoreUtil.getInstance().get("MHY-YS-region");
    let role_id = await kvStoreUtil.getInstance().get("MHY-YS-UID");
    if(server===null || server === undefined || role_id === null || role_id === undefined){
      log.warn("dailyNoteApi",`server or role_id is missing. server:${server} role_id:${role_id}`)
      let magr = await mysApiMagr.getInstance()
      let api = await getGameRecordCard.getInstance()
      let resp = await magr.getData(api)
      let res = resp.data["list"] as Array<object>
      for(let i=0;i<res.length;i++)
      {
        if(res[i]["game_id"]===2){//原神
          await kvStoreUtil.getInstance().put("MHY-YS-UID",res[i]["game_role_id"])
          await kvStoreUtil.getInstance().put("MHY-YS-region",res[i]["region"])
          await kvStoreUtil.getInstance().put("MHY-YS-region_name",res[i]["region_name"])
          server = res[i]["region"]
          role_id = res[i]["game_role_id"]
          log.info("dailyNoteApi",`update server and role_id of Genshin.`)
          break;
        }
      }
    }
    return new dailyNoteApi(server,role_id);
  }
  constructor(server:string,role_id:string) {
    super(
      {
        url:"https://api-takumi-record.mihoyo.com/game_record/app/genshin/api/dailyNote",
        query:{
          server:server,
          role_id:role_id
        }
      }
    );

  }
  getQueryStr():string{
    if(this.query.get("server") === undefined || this.query.get("server") === null){
      log.error(dailyNoteApi.name,"missing key of server")
      throw new Error("missing key of server")
    }
    if(this.query.get("role_id") === undefined || this.query.get("role_id") === null){
      log.error(dailyNoteApi.name,"missing key of role_id")
      throw new Error("missing key of role_id")
    }
    return `role_id=${this.query.get("role_id")}&server=${this.query.get("server")}`
  }
}